package com.iseastar.guojiang.register;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.loc.BaiduLocationService;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.RegisterBusinessTrainingBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;

import java.util.ArrayList;


/**
 * 业务培训
 * Created by lidandan on 2018/5/2.
 */
public class RegisterBusinessTrainingActivity extends BaseActivity2 implements View.OnClickListener {
    private TextView mMakeStatusTV;
    private TextView mMakeStatusContentTV;
    private TextView mMakePhoneTV;
    private TextView mPlaceTrainingTV;
    private TextView mDetailsTrainingPlacesTV;
    private TextView mMakeTrainingTV;
    private TextView mBaiduMapTV;
    private UserBean userBean;
    private BDLocation bdLocation;
    private ArrayList<RegisterBusinessTrainingBean> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        userBean = (UserBean) getIntent().getSerializableExtra("item");
        if (userBean == null) {
            userBean = AppCache.getUser();
        }
        super.onCreate(savedInstanceState);
        if (userBean != null && userBean.getCourierAudit() != null && userBean.getCourierAudit().getTrainStatus() == 1) {
            //百度定位
            showLoadingDialog("正在定位");
            Intent intent = new Intent();
            intent.setClass(getContext(), BaiduLocationService.class);
            intent.putExtra("msgId", com.iseastar.guojiang.consts.MsgID.location_baidu);
            startService(intent);
        } else {
            updateViewForStatus();
        }

    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.register_business_training_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("业务培训");
        mMakeStatusTV = (TextView) findViewById(R.id.make_status_tv);
        mMakeStatusContentTV = (TextView) findViewById(R.id.make_status_content_tv);
        mMakePhoneTV = (TextView) findViewById(R.id.make_phone_tv);
        mPlaceTrainingTV = (TextView) findViewById(R.id.place_training_tv);
        mDetailsTrainingPlacesTV = (TextView) findViewById(R.id.details_training_places_tv);
        mBaiduMapTV = (TextView) findViewById(R.id.baidu_map);
        mBaiduMapTV.setOnClickListener(this);
        mMakeTrainingTV = (TextView) findViewById(R.id.make_training_tv);
        mMakeTrainingTV.setOnClickListener(this);
    }

    /**
     * 根据预约状态更新界面
     */
    public void updateViewForStatus() {
        if (userBean == null || userBean.getCourierAudit() == null) {
            return;
        }
        //未预约
        if (userBean.getCourierAudit().getTrainStatus() == 1) {
            mMakeStatusTV.setText("请预约培训");
            mMakeStatusContentTV.setText("完成预约后，请5个工作日内准时到达如下地点完成培训。");
            mMakePhoneTV.setVisibility(View.VISIBLE);
            mMakeTrainingTV.setVisibility(View.VISIBLE);
        }//预约成功
        else if (userBean.getCourierAudit().getTrainStatus() == 2) {
            mMakeStatusTV.setText("已预约培训");
            mMakeStatusContentTV.setText("请5个工作日内准时到达如下地点完成培训。");
            mMakePhoneTV.setVisibility(View.VISIBLE);
            mMakeTrainingTV.setVisibility(View.GONE);
            mPlaceTrainingTV.setText("培训地点：" + userBean.getCourierAudit().getWareHouseName());
            mDetailsTrainingPlacesTV.setText(userBean.getCourierAudit().getTrainAddress());
        }//培训成功
        else if (userBean.getCourierAudit().getTrainStatus() == 3) {
            mMakeStatusTV.setText("恭喜您，已完成培训");
            mMakeStatusContentTV.setText("请尽快领取物料，上岗赚钱吧！");
            mMakePhoneTV.setVisibility(View.GONE);
            mMakeTrainingTV.setVisibility(View.GONE);
            mPlaceTrainingTV.setText("培训地点：" + userBean.getCourierAudit().getWareHouseName());
            mDetailsTrainingPlacesTV.setText(userBean.getCourierAudit().getTrainAddress());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.baidu_map:
                if (items == null || items.size() <= 0 || items.get(0).getLatitude() <= 0 || items.get(0).getLongitude() <= 0) {
                    showToast("培训坐标有误");
                    return;
                }
                Intent intent = new Intent(this, RegistegerBusinessTrainingMapActivity.class);
                intent.putExtra("longitude", items.get(0).getLongitude());
                intent.putExtra("latitude", items.get(0).getLatitude());
                startActivity(intent);
                break;
            case R.id.make_training_tv:
                if (items == null || items.size() <= 0) {
                    return;
                }
                AppHttp.getInstance().courierBusinessTraining(items.get(0).getWareHouseName(), items.get(0).getTrainAddress(), items.get(0).getLongitude(), items.get(0).getLatitude());
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 获取百度定位
            case com.iseastar.guojiang.consts.MsgID.location_baidu:
                bdLocation = (BDLocation) msg.obj;
                if (bdLocation != null && isNotEmpty(bdLocation.getProvince()) && isNotEmpty(bdLocation.getCity())) {
                    showLoadingDialog("查询中");
                    AppHttp.getInstance().courierBusinessTrainingAddress(bdLocation.getProvince(), bdLocation.getCity(), bdLocation.getDistrict());
                } else {
                    cancelLoadingDialog();
                }
                return false;
            //培训地点
            case MsgID.courier_business_training_address:
                cancelLoadingDialog();
                final ReqResult<RegisterBusinessTrainingBean> result = JSON.parseList(msg.obj, RegisterBusinessTrainingBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            items = result.getResultList();
                            updateViewForStatus();
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return false;
            //预约成功后回调
            case MsgID.courier_business_training:
                cancelLoadingDialog();
                final ReqResult<RegisterBusinessTrainingBean> result1 = JSON.parseList(msg.obj, RegisterBusinessTrainingBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result1)) {
                            if (userBean == null) {
                                userBean = AppCache.getUser();
                            }
                            UserBean.CourierAudit courierAudit = userBean.getCourierAudit();
                            if (courierAudit == null) {
                                courierAudit = new UserBean.CourierAudit();
                            }
                            if (items != null && items.size() > 0) {
                                courierAudit.setTrainAddress(items.get(0).getTrainAddress());
                                courierAudit.setLatitude(items.get(0).getLatitude());
                                courierAudit.setLongitude(items.get(0).getLongitude());
                                courierAudit.setWareHouseName(items.get(0).getWareHouseName());
                            }
                            courierAudit.setTrainStatus(2);
                            userBean.setCourierAudit(courierAudit);
                            AppCache.setUser(userBean);
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else {
                            showToast(result1.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }

    }
}
